<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');
if(!class_exists('CI_Model')) { class CI_Model extends Model {} }

class Content_model extends CI_Model {
	public function __construct() {
	   parent::__construct();
	   $this->url = $this->uri->segment(1);
	}
        function translate($var=''){ // для curl
            // писок соответствий
            $NpjLettersFrom = array("а", "б", "в", "г", "д", "е", "ё", "з", "и", "к", "л", "м", "н", "о", "п", "р", "с", "т", "у", "ф", "ц", "ы", "й", "ж", "х", "ч", "ш", "щ", "э", "ю", "я");
            $NpjLettersTo   = array("a","b","v","g","d","e", "jo", "z","i","k","l","m","n","o","p","r","s","t","u","f","c","y", "jj", "zh", "kh", "ch", "sh", "shh", "je", "ju", "ja", "", "");

            $var = trim(strip_tags($var));  // чистим
            $var = mb_strtolower($var); // to low case
            $var = preg_replace( "/\s+/ms", "_", $var ); // замена пробелов
            $var = str_replace( $NpjLettersFrom, $NpjLettersTo, $var ); // замена соответсвий
            $var = preg_replace("/[^a-z0-9-_]+/mi", "", $var); // очистка от спецсимволов

            return $var;
        }
		function getTree() { // выпадающий список для админки
			$pages = $this->db->select('page_title, page_path, page_id, page_curl')->from('content')->order_by('page_path', 'asc')->get()->result_array();
			return $pages;
		}
		function updateAllPath($path = ":00000:", $id = 0) { // переиндексация
			$pages = $this->db->select('page_id, page_title, page_path, page_parent, page_order')->from('content')->where('page_parent', $id)->get()->result_array();
			if ($id == 0) {
				foreach ($_COOKIE as $k => $v) {
					if (mb_substr_count($k, "js_tr_") > 0) {
						unset($_COOKIE[$k]);
						delete_cookie(str_replace("js_", "", $k));
					}
				}
			}
			// &.order.#:id:
			if (sizeof($pages) > 0) {
				foreach ($pages as $page) {
					if ($id == 0) {
						set_cookie('tr_'.$page['page_id'], 'show', 604800, '/');
					}
					$path_new = $path;
					$path_new .= "&.".to5zeros($page['page_order']).".#";
					$path_new .= ":".to5zeros($page['page_id']).":";
					$this->db->where('page_id', $page['page_id'])->update('content', array('page_path' => $path_new));
					$this->updateAllPath($path_new, $page['page_id']);
				}
			}
		}
		function get_menu_from($id, $levels = 1, $text=false, $only_active = true) {
			if ($levels > 0) {
				$this->db->select('page_id, page_curl, page_title, page_desc, page_function, page_model, page_status, page_parent'.($text==true?", page_text":", page_active"))->from('content');
				if ($only_active === true) $this->db->where('page_status', '1');
				$pages = $this->db->where('page_parent', $id)->order_by('page_path', 'asc')->get()->result_array();
				$levels = $levels - 1;
				foreach ($pages as $page) {
					$out[$page['page_id']] = $page;
					$get = $this->get_menu_from($page['page_id'], $levels, $text);
					if (sizeof($get) > 0) $out[$page['page_id']]['childs'] = $get;
				}
				return $out;
			}

		}
		function getCrumps($path, $url, $none) {
			$allpages = $this->db->select('page_title, page_id')->from('content')->get()->result_array();
			foreach ($allpages as $allpages_one) {
				$book[$allpages_one['page_id']] = array('title' => $allpages_one['page_title'], 'id' => $allpages_one['page_id']);
			}
			$cr = explode("&", $path);
			$crumps = "";
			foreach ($cr as $cr_one) {
				preg_match_all('#:[0-9]+:#', $cr_one, $num);
				$num = intval(str_replace(":", "", $num[0][0]));
				if ($num != "0" && $num != $none)
				$crumps .= " &raquo; <a href='/".$url."/".$book[$num]['id']."/'>".$book[$num]['title']."</a>";
			}
			return $crumps;
		}
		function listing_all(){
			$parent = $this->db->select()->from('content')->where('page_curl',$this->url)->limit(1)->get()->row_array();
			$this->db->select()->from('content')->where('page_parent',$parent['page_id']);
			$data['pages'] = $pages = $this->db->order_by('page_order')->get()->result_array();
			foreach($pages as $key=>$page){
				$out['text'] .= "<h2>";
				$out['text'] .= $page['page_title'];
				$out['text'] .= "</h2>";
				if($page['page_curl'] == 'faq'){
					$limit = 10;
				} else {
					$limit = 5;
				}
				$temp = $this->listing($page['page_id'],$limit);
				$out['text'] .= $temp['text'];
				switch($page['page_curl']){
					case "faq":
						$out['text'] .= "<a href='/".$page['page_curl']."/'>Все вопросы и ответы</a>";
						break;
					case "reviews":
						$out['text'] .= "<a href='/".$page['page_curl']."/'>Все статьи</a>";
						break;
					default:
						$out['text'] .= "<a href='/".$page['page_curl']."/'>Весь список</a>";
						break;
				}
				
			}
			return $out;
		}
		function listing($id = "",$limit=FALSE){
			if(is_array($id)){
				$parent = $this->db->select()->from('content')->where('page_curl',$this->url)->limit(1)->get()->row_array();
				$id = $parent['page_id'];
			} 
			$this->db->select()->from('content')->where('page_parent',$id);
			$this->db->order_by('page_order');
			if($limit){
				$this->db->limit($limit);
			}
			$data['pages'] = $pages = $this->db->get()->result_array();
			$out['text'] = $this->load->view("articles_faq", $data, true);
			return $out;
			
		}
}
?>